package jjn.carl.hashtable;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Jjn
 * @since 2023/7/3 17:11
 */
public class LeetCode202 {
    public boolean isHappy(int n) {
        Set<Integer> existed = new HashSet<>();
        while (n != 1 && existed.add(n)) {
            n = getNextNumber(n);
        }
        return n == 1;
    }
    
    private int getNextNumber(int n) {
        int total = 0;
        while (n > 0) {
            int rest = n % 10;
            total += rest * rest;
            n = n / 10;
        }
        return total;
    }
    
    public static void main(String[] args) {
        System.out.println("new LeetCode202().isHappy(19) = " + new LeetCode202().isHappy(19));
        System.out.println("new LeetCode202().isHappy(2) = " + new LeetCode202().isHappy(2));
    }
}
